/**
 * Created by cjh1 on 2016/8/15.
 */

//$('#mycarousel').slider();
var loading;
var comCondition = {
    page: 1,
    url: '',
    area: '',
    indus: ''
};

var userCondition = {
    page: 1,
    url: '',
    school: '',
    professional: ''
};

// comAjax(true);
// userAjax(true);

$('#area-list').selectable({
    item: 'li',
    selected: function (e, item) {
        comCondition.page = 1;
        comCondition.area = $(item).data('value');
        $('.cpn-btn .confirm').on('click',function(){
            comAjax(true);
            if(comCondition.area==''){
                $('.header-selected').text('-不限');
            }
            else{
                $('.header-selected').text('-'+comCondition.area);
            }
            $('.cpn-header-title1').css('color','#959595');
            $('.cpn-header-area').attr('src','/uploads/demo/pull_down.png');
            $('.condition-shade').fadeOut(200);
            $('.cpn-select').slideUp(200);
        });

    }
});
$('#industry-list').selectable({
    item: 'li',
    selected: function (e, item) {
        comCondition.page = 1;
        comCondition.indus = $(item).data('value');
        $('.cpn-btn .confirm').on('click',function(){
            comAjax(true);
            if(comCondition.indus == ''){
                $('.header-selected2').text('-不限');
            }
            else{
                $('.header-selected2').text('-'+comCondition.indus);
            }
            $('.cpn-header-title2').css('color','#959595');
            $('.cpn-header-type').attr('src','/uploads/demo/pull_down.png');
            $('.condition-shade').fadeOut(200);
            $('.cpn-select').slideUp(200);
        });
    }
});

$('#condition-school-list').selectable({
    item: 'li',
    selected: function (e, item) {
        userCondition.page = 1;
        userCondition.school = $(item).data('value');
        $('.cpn-btn .confirm').on('click',function(){
            userAjax(true);
            if(userCondition.school==''){
                $('.header-selected3').text('-不限');
            }
            else{
                $('.header-selected3').text('-'+userCondition.school);
            }
            $('.cpn-header-title1').css('color','#959595');
            $('.cpn-header-area').attr('src','/uploads/demo/pull_down.png');
            $('.condition-shade').fadeOut(200);
            $('.cpn-select').slideUp(200);
        });
    }
});

$('#condition-profession-list').selectable({
    item: 'li',
    selected: function (e, item) {
        userCondition.page = 1;
        userCondition.professional = $(item).data('value');
        $('.cpn-btn .confirm').on('click',function(){
                userAjax(true);
                if(userCondition.professional==''){
                    $('.header-selected4').text('-不限');
                }
                else{
                    $('.header-selected4').text('-'+userCondition.professional);
                }
            $('.cpn-header-title2').css('color','#959595');
            $('.cpn-header-type').attr('src','/uploads/demo/pull_down.png');
            $('.condition-shade').fadeOut(200);
            $('.cpn-select').slideUp(200);
        });
    }
});

 $('.cpn-header-area').on('click',function(){
        $('.cpn-header-area').attr('src','/uploads/demo/pull_up.png');
        $('.cpn-type').hide();
        $('.condition-shade').fadeIn(200);
        $('.cpn-select').fadeIn(300);
        $('.cpn-area').fadeIn(300);
        $('.cpn-header-title1').css('color','#298aff');
    });
    $('.cpn-header-type').on('click',function(){
        $('.cpn-header-type').attr('src','/uploads/demo/pull_up.png');
        $('.cpn-area').hide();
        $('.condition-shade').fadeIn(200);
        $('.cpn-select').fadeIn(300);
        $('.cpn-type').fadeIn(300);
        $('.cpn-header-title2').css('color','#298aff');
    });
    $('.cpn-select .cancel').on('click',function(){
        $('.condition-shade').fadeOut(200);
        $('.cpn-select').slideUp(200);
        $('.cpn-header-title1').css('color','#959595');
        $('.cpn-header-title2').css('color','#959595');
        $('.cpn-header-area').attr('src','/uploads/demo/pull_down.png');
        $('.cpn-header-type').attr('src','/uploads/demo/pull_down.png');


    });


function userAjax(updatePage) {
    loading = eui.load(2, {
        fix: true,
        scrollbar: true
    });
    $('#load-more-user').hide();
    $.ajax({
        url: '/user/listpage',
        data: {
            page: userCondition.page,
            json: 1,
            school: userCondition.school,
            professional: userCondition.professional,
            pageSize: 15
        },
        type: 'GET',
        dataType: 'json',
        success: function (obj) {
            eui.close(loading);
            $('#load-more-user').show();
            userView(obj);
            if (updatePage) {
                userPage(obj);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            eui.close(loading);
            eui.msg('系统出错');
        }
    });
}

function userView(obj) {
    var list = obj['list'];
    var $list = $('#user-list');
    if (userCondition.page === 1) {
        $list.empty();
    }


    if (list.length === 0) {
        $('#search-empty').show();
        $('#load-more-user').hide();
        $('#user-list').hide();
        return;
    }

    $('#search-empty2').hide();
    $('#load-more-user').show();
    $('#com-list').show();

    for (var i = 0; i < list.length; i++) {
        var user = list[i];
        var isTop = userCondition.page === 1 && i < 3;
        var html = '<li class="list-item">'
            + '<img class="item-logo" src="' + user.photo + '">'
            + '<div class="item-info">'
            + '<a class="item-title" href="/user/detail?id=' + user.id + '"><h3>' + user.name + '</h3></a>'
            + (function () {
                if (!isTop) {
                    return '<div class="info"></div>'; // 占个位置，排版好看
                }
                var arr = ['人气王', '人气王第二名', '人气王第三名'];
                return '<div class="info"><i class="icon icon-tag"></i> <span class="tag-item">' + arr[i] + '</span></div> ';
            })()
            + '<div class="info">'
            + function () {
                if (user.school) {
                    return '<span class="info-item">' + user.school + '</span>';
                } else {
                    return '';
                }
            }()
            + function () {
                if (user.professional) {
                    return '<span class="info-item">' + user.professional + '</span>';
                } else {
                    return '';
                }
            }()
            + '</div>'
            + '</div>'
            + function () {
                if (page.type === '1' || page.type === '3') { // 首页和人气王
                    var disabled = user .is_parise === 1 ? 'disabled' : '';
                    return '<div class="right-top"><a class="parise-user ' + disabled + '" href="#" data-id="' + user.id + '"><i class="icon icon-praise"></i> <span class="parise-num">' + user.parise + '</span></a></div>';
                } else { // Like
                    return '<div><a class="item-like like-user" href="#" data-id="' + user.id + '"><img class="like-img" src="/public/asset/img/like-img-text.jpg"></a></div>';
                    //return ''; // 手机端因为企业无法登陆，不能LIKE学生
                }
            }()
            + '</li>';
        var $li = $(html);
        $list.append($li);
    }
}

function comAjax(updatePage) {
    var loading = eui.load(2);
    $('#load-more-com').hide();
    $.ajax({
        //url: searchCondition.getUrl(),
        url: '/cpn/listpage',
        data: {
            page: comCondition.page,
            json: 1,
            pageSize: 15,
            area: comCondition.area,
            indus: comCondition.indus
        },
        type: 'GET',
        dataType: 'json',
        success: function (obj) {
            eui.close(loading);
            $('#load-more-com').show();
            comView(obj);
            if (updatePage) {
                comPage(obj);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            eui.close(loading);
            eui.msg('系统出错');
        }
    });
}

function comPage(obj) {
    $('#load-more-com').loadmore({
        page: parseInt(obj.page),
        totalPage: obj.maxPage,
        onPage: function (page) {
            comCondition.page = page;
            comAjax();
        }
    });
    /*$('#com-list-page').page({
        page: parseInt(obj.page),
        pageSize: 9,
        totalPage: obj.maxPage,
        onPage: function (page) {
            comCondition.page = page;
            loading = eui.load(2, {
                scrollbar: true
            });
            comAjax();
            location.href = '#data-box';
        }
    });*/
}

function userPage(obj) {
    $('#load-more-user').loadmore({
        page: parseInt(obj.page),
        totalPage: obj.maxPage,
        onPage: function (page) {
            userCondition.page = page;
            userAjax();
        }
    });
}

function comView(obj) {
    var list = obj['list'];
    var $list = $('#com-list');
    if (comCondition.page === 1) {
        $list.empty();
    }

    if (list.length === 0) {
        $('.search-empty').show();
        $('#page-list').hide();
        $('#user-list').hide();
        return;
    }

    $('#search-empty').hide();
    $('#page-list').show();
    $('#user-list').show();
    for (var i = 0; i < list.length; i++) {
        var com = list[i];
        var isTop = comCondition.page === 1 && i < 3;
        var html = '<li class="list-item">'
            + '<img class="item-logo" src="' + com.logo + '">'
            + '<div class="item-info">'
            + '<a class="item-title" href="/cpn/detail?id=' + com.id + '"><h3>' + com.name + '</h3></a>'
            + (function () {
                if (com.area) {
                    return '<div class="info"><i class="icon icon-position"></i> ' + com.area + '</div>';
                } else {
                    return '';
                }
            })()
            + '<div class="info">'
            + '<i class="icon icon-tag"></i> '
            + function () {
                if (com.industry) {
                    return '<span class="tag-item">' +com.industry + '</span>';
                } else {
                    return '';
                }
            }()
            + (function () {
                if (!isTop || page.type === '2') {
                    return '';
                }
                var arr = [' 人气王', ' 人气王第二名', ' 人气王第三名'];
                return '<span class="tag-item">' + arr[i] + '</span>';

            })()
            + '</div>'
            + function () {
                return '<div class="item-desc line1">' + com.desc + '</div></div>';
            }()
            + function () {
                if (page.type === '1' || page.type === '3') { // 首页和人气王
                    var disabled = com.is_parise === 1 ? 'disabled' : ''
                    return '<div class="right-top"><a class="parise-com ' + disabled + '" href="#" data-id="' + com.id + '"><i class="icon icon-praise"></i> <span class="parise-num">' + com.parise + '</span></a></div>';
                } else { // Like
                    //console.log(page);
                    return '<div><a class="item-like like-com clear" href="#" data-id="' + com.id + '"><img class="like-img" src="/public/asset/img/like-img-text.jpg"></a></div>';
                }

            }()
            //+
            + '</li>';
        var $li = $(html);
        $list.append($li);

    }

}
function comView2(obj) {
    var list = obj['list'];
    var $list = $('#com-list');
    for (var i = 0; i < 8; i++) {
        var com = list[i];
        var isTop = comCondition.page === 1 && i < 3;
        var html = '<li class="list-item">'
            + '<a href="/cpn/detail?id=' + com.id + '"><img class="img" src="' + com.logo + '">'
            + '<div><div class="title">' + com.name + '</div></a>'
            + '</li>';
        var $li = $(html);
        $list.append($li);
    }

}
function comAjax2(updatePage) {
    $.ajax({
        //url: searchCondition.getUrl(),
        url: '/cpn/listpage',
        data: {
            page: comCondition.page,
            json: 1,
            pageSize: 15,
            area: comCondition.area,
            indus: comCondition.indus
        },
        type: 'GET',
        dataType: 'json',
        success: function (obj) {
            comView2(obj);
            //location.href = "#data-box";
            if (updatePage) {
                comPage(obj);
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            eui.msg('系统出错');
        }
    });
}
function userAjax2(updatePage) {
    $.ajax({
        url: '/user/listpage',
        data: {
            page: userCondition.page,
            json: 1,
            school: userCondition.school,
            professional: userCondition.professional,
            pageSize: 15
        },
        type: 'GET',
        dataType: 'json',
        success: function (obj) {
            eui.close(loading);

            userView2(obj);
            if (updatePage) {
                userPage(obj);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            eui.msg('系统出错');
        }
    });
}

function userView2(obj) {
    var list = obj['list'];
    var $list = $('#user-list');
    for (var i = 0; i < 8; i++) {
        var user = list[i];
        var isTop = userCondition.page === 1 && i < 3;
        var html = '<li class="list-item">'
            + '<a href="/user/detail?id=' + user.id + '"><img class="img" src="' + user.photo + '">'
            + '<span class="title">' + user.name + '</span></a>'
            + '</li>';
        var $li = $(html);
        $list.append($li);
    }
}

// 点赞企业
$(document).on('click', '.parise-com', function (e) {
    e.preventDefault();
    var $this = $(this);
    var id = $(this).data('id');

    if ($this.hasClass('disabled')) {
        eui.msg('您已经点过赞了');
        return;
    }
    $.ajax({
        url: '/user/parise',
        data: {
            aim_id: id,
            aim_type: 2 // 1赞学生2赞企业
        },
        type: 'POST',
        dataType: 'json',
        success: function (obj) {
            if (obj.code === 0) {
                //msg('操作成功');
                var $num = $this.find('.parise-num');
                $num.text(parseInt($num.text()) + 1);
                $this.addClass('disabled')
            } else {
                eui.msg(obj.msg);
            }
        },
        error: function () {
            eui.msg('系统出错');
        }
    })
});

// 点赞用户
$(document).on('click', '.parise-user', function (e) {
    e.preventDefault();

    var $this = $(this);
    var id = $(this).data('id');
    if ($this.hasClass('disabled')) {
        eui.msg('您已经点过赞了');
        return;
    }
    $.ajax({
        url: '/user/parise',
        data: {
            aim_id: id,
            aim_type: 1 // 1赞学生2赞企业
        },
        type: 'POST',
        dataType: 'json',
        success: function (obj) {
            if (obj.code === 0) {
                var $num = $this.find('.parise-num');
                $num.text(parseInt($num.text()) + 1);
                $this.addClass('disabled')
            } else {
                eui.msg(obj.msg);
            }
        },
        error: function () {
            eui.msg('系统出错');
        }
    })
});
var curComId;
// Like企业
$(document).on('click', '.like-com', function (e) {
    e.preventDefault();

    var $this = $(this);
    curComId = $(this).data('id');

    // 游客提示登录
    if (page.userType == '') {
        eui.alert('登录后才能LIKE噢~', {
            btn: '快去登陆 >>'
        }, function (id) {
            eui.close(id);
            window.location.href = '/page/login';
        });
        return;
    }
    if (page.userType !== '' && page.userType !== '1') {
        eui.msg('只有学生才可以LIKE企业');
        return;
    }
    console.log(page.userType,'12')

    $('#like-dialog').dialog();

    if (page.userType !== '1') {
        //$('#quick-list').hide();
    } else {

        /*$('#like-wrap .dialog-shade2').fadeIn(400);
        $('#box-like-login').hide();
        $('.box-like').fadeIn(400);*/
    }
});

$('#like-btn').on('click', function () {
    if (page.userType !== '1') {
        eui.msg('请先登录');
        return;
    }

    var value = $('#like-text').val();
    if (!value) {
        eui.msg('请输入内容');
        return;
    }
    $.ajax({
        url: '/user/like',
        data: {
            id: curComId,
            like_msg: value
        },
        type: 'POST',
        dataType: 'json',
        success: function (obj) {
            if (obj.code === 0) {
                $('#like-dialog').dialog('hide');
                eui.msg('LIKE成功');
            } else {
                eui.msg(obj.msg);
            }
        },
        error: function () {
            eui.msg('系统出错');
        }
    })
});
$('.box-like .cancel').on('click', function() {
    $('#like-dialog').dialog('hide');
});
// 快速输入
$('#quick-list').on('click', 'a', function (e) {
    e.preventDefault();
    $('#like-text').val($(this).text());
});

var curStdId;
// Like学生
$(document).on('click', '.like-user', function (e) {
    e.preventDefault();
    eui.alert('只有企业才可以 LIKE 噢~', {
        btn: false
    });
    return; // 由于企业不能登陆，无法LIKE

    var $this = $(this);
    curStdId = $(this).data('id');
    if (page.userType !== '' && page.userType !== '2') {
        eui.msg('只有企业才可以LIKE学生噢~', {
            time: false,
            shade: 0.3,
            shadeClose: true
        });
        return;
    }
    if (page.userType == '') {
        eui.alert('登录后才能LIKE噢~', {
            btn: '快去登陆 >>'
        }, function (id) {
            eui.close(id);
            window.location.href = '/page/login';
        });
        return;
    }
    if (page.userType !== '1') {
        $('#quick-list').hide();
    } else {
        $('#like-wrap .dialog-shade2').fadeIn(400);
        $('#box-like-login').hide();
        $('.box-like').fadeIn(400);
    }
});

$('#like-btn2').on('click', function () {
    if (page.userType !== '2') {
        eui.msg('请先登录');
        return;
    }

    var value = $('#like-text2').val();
    if (!value) {
        eui.msg('请输入内容');
        return;
    }
    $.ajax({
        url: '/company/like/like',
        data: {
            id: curStdId,
            like_msg: value
        },
        type: 'POST',
        dataType: 'json',
        success: function (obj) {
            if (obj.code === 0) {
                 $('.box-like').hide();
                 $('#like-wrap .dialog-shade2').fadeOut(400);
                eui.msg('LIKE成功');
            } else {
                eui.msg(obj.msg);
            }
        },
        error: function () {
            eui.msg('系统出错');
        }
    })
});
